﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CustomersSuppliersBusinessLayer;
using CustomersSuppliersDataLayer;
using System.Collections;
using WebHelper.Helpers;

namespace CustomersSuppliersWebApplication
{
    public partial class WebFormCountries : WebFormBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitializeTelerikRequest(this.RadTextBoxSearch, "onkeyup");
                this.PageTitle = "الدول";
            }
        }
        protected void RadTextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            this.getCountrys();
            this.RadGridResults.Rebind();
        }
        protected void RadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.getCountrys();
        }
        protected void RadGridResults_ItemCreated(object sender, GridItemEventArgs e)
        {
            //pager
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                Button changePageSize = (Button)e.Item.FindControl("ChangePageSizeLinkButton");
                changePageSize.Text = "تغيير";
                Button goTO = (Button)e.Item.FindControl("GoToPageLinkButton");
                goTO.Text = "تغيير";
                Label itesNumberLabel = (Label)e.Item.FindControl("ChangePageSizeLabel");
                itesNumberLabel.Text = "          عدد الصفوف :";
                Label goTOLabel = (Label)e.Item.FindControl("GoToPageLabel");
                goTOLabel.Text = "صفحة :";
                Label ofLabel = (Label)e.Item.FindControl("PageOfLabel");
                ofLabel.Text = "من " + item.Paging.PageCount.ToString();
            }
        }
        protected void RadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
            //set default value
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //e.Canceled = true;
                //System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                //newValues[BLCountry.PropertiesNames.Country_StartDate] = DateTime.Now.Date;
                //e.Item.OwnerTableView.InsertItem(newValues);
            }

        }
        protected void RadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLCountry Country = new BLCountry();
            if (newValues[BLCountry.PropertiesNames.Country_Code] != null)
            {
                Country.Country_Code = newValues[BLCountry.PropertiesNames.Country_Code].ToString();
            }
            if (newValues[BLCountry.PropertiesNames.Country_NameAr] != null && newValues[BLCountry.PropertiesNames.Country_NameAr].ToString().Trim() != "")
            {
                Country.Country_NameAr = newValues[BLCountry.PropertiesNames.Country_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLCountry.PropertiesNames.Country_NameEn] != null)
            {
                Country.Country_NameEn = newValues[BLCountry.PropertiesNames.Country_NameEn].ToString();
            }
            if (newValues[BLCountry.PropertiesNames.Country_Notes] != null)
            {
                Country.Country_Notes = newValues[BLCountry.PropertiesNames.Country_Notes].ToString();
            }
            Country.Country_Search = StringHelper.ToUniqueStatement(Country.Country_Code + Country.Country_NameAr + Country.Country_NameEn + Country.Country_Notes);
            Country.AutoSave();
            if (Country.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLCountry Country = new BLCountry();
            Country.Country_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLCountry.PropertiesNames.Country_ID].ToString());
            Country.LoadByIdentifiers();
            if (newValues[BLCountry.PropertiesNames.Country_Code] != null)
            {
                Country.Country_Code = newValues[BLCountry.PropertiesNames.Country_Code].ToString();
            }
            if (newValues[BLCountry.PropertiesNames.Country_NameAr] != null && newValues[BLCountry.PropertiesNames.Country_NameAr].ToString().Trim() != "")
            {
                Country.Country_NameAr = newValues[BLCountry.PropertiesNames.Country_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLCountry.PropertiesNames.Country_NameEn] != null)
            {
                Country.Country_NameEn = newValues[BLCountry.PropertiesNames.Country_NameEn].ToString();
            }
            if (newValues[BLCountry.PropertiesNames.Country_Notes] != null)
            {
                Country.Country_Notes = newValues[BLCountry.PropertiesNames.Country_Notes].ToString();
            }
            Country.Country_Search = StringHelper.ToUniqueStatement(Country.Country_Code + Country.Country_NameAr + Country.Country_NameEn + Country.Country_Notes);
            Country.AutoSave();
            if (Country.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            BLCountry Country = new BLCountry();
            Country.Country_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLCountry.PropertiesNames.Country_ID].ToString());
            Country.DeleteByIdentifiers();
            if (Country.Error == null)
            {
                this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحذف", NotificationIconType.Error);
                return;
            }
        }
        protected void RadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //width
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)(editMan.GetColumnEditor(BLCountry.PropertiesNames.Country_Notes));
                editor.TextBoxMode = TextBoxMode.MultiLine;
                editor.TextBoxControl.Width = Unit.Pixel(300);
                editor.TextBoxControl.Height = Unit.Pixel(40);
            }
        }

        private void getCountrys()
        {
            var items = from i in CashVariables.CountryCollection
                        where i.Country_Search.Contains(StringHelper.ToUniqueStatement(this.RadTextBoxSearch.Text))
                        select i;
            BLCountryCollection CountryCollection = new BLCountryCollection(items);
            this.RadGridResults.DataSource = CountryCollection;
            this.RadGridResults.MasterTableView.DataKeyNames = BLCountry.PropertiesNames.Identifiers;
        }
    }
}